package com.blockmeta.bbs.immodule.utils;

import android.app.ActivityManager;
import android.content.Context;

import com.blockmeta.bbs.businesslibrary.launch.IMConfigYMAL;
import com.blockmeta.bbs.businesslibrary.pojo.IMConfigPOJO;

import java.util.HashMap;
import java.util.List;

/**
 * Created by author on 2019/3/20.
 */

public class IMUtils {



    /**
     * 判断服务是否运行
     */
    private boolean isServiceRunning(Context context, final String className) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> info = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (info == null || info.size() == 0) return false;
        for (ActivityManager.RunningServiceInfo aInfo : info) {
            if (className.equals(aInfo.service.getClassName())) return true;
        }
        return false;
    }

    public enum UserLevel {
        USERLEVEL_XIAOBAI("小白", 9),
        USERLEVEL_SHUISHOU("水手", 10),
        USERLEVEL_CHUANYUAN("船员", 31),
        USERLEVEL_DUIZHANG("队长", 11),
        USERLEVEL_FUCHUANZHANG("副船长", 12),
        USERLEVEL_CHUANZHANG("船长", 29),
        USERLEVEL_HAIDAOWANG("海盗王", 14),
        USERLEVEL_HAIDAOHAO("海盗号", 7);

        static private HashMap<Integer, UserLevel> m_map = new HashMap<>();

        static {
            for (UserLevel status : UserLevel.values()) {
                m_map.put(status.getType(), status);
            }
        }

        protected String m_value;
        protected int m_type;

        UserLevel(String value, int type) {
            m_value = value;
            m_type = type;
        }

        public String getValue() {
            return m_value;
        }

        public int getType() {
            return m_type;
        }

        public boolean isEqual(UserLevel type) {
            return m_value.equals(type.getValue());
        }

        public static UserLevel getUserLevel(int type) {
            UserLevel tmp = m_map.get(type);
            if(tmp == null) tmp = USERLEVEL_CHUANYUAN;
            return tmp;
        }

        public static int compareLevel(int level1, int level2){
            return getUserLevel(level1).ordinal() >= getUserLevel(level2).ordinal() ? 1 : -1;
        }

        public static boolean matchLevel(int userLevel, int groupLevel){
            boolean isMatch = false;
            switch (groupLevel){
                case 0:
                    isMatch = true;
                    break;
                case 1:
                    isMatch = userLevel > USERLEVEL_XIAOBAI.m_type;
                    break;
                case 2:
                    isMatch = (userLevel == USERLEVEL_CHUANYUAN.m_type || userLevel == USERLEVEL_DUIZHANG.m_type || userLevel == USERLEVEL_FUCHUANZHANG.m_type
                            || userLevel == USERLEVEL_CHUANZHANG.m_type || userLevel == USERLEVEL_HAIDAOWANG.m_type);
                    break;
                case 3:
                    isMatch = (userLevel == USERLEVEL_DUIZHANG.m_type || userLevel == USERLEVEL_FUCHUANZHANG.m_type
                            || userLevel == USERLEVEL_CHUANZHANG.m_type || userLevel == USERLEVEL_HAIDAOWANG.m_type);
                    break;
                case 4:
                    isMatch = (userLevel == USERLEVEL_FUCHUANZHANG.m_type
                            || userLevel == USERLEVEL_CHUANZHANG.m_type || userLevel == USERLEVEL_HAIDAOWANG.m_type);
                    break;
                case 5:
                    isMatch = (userLevel == USERLEVEL_CHUANZHANG.m_type || userLevel == USERLEVEL_HAIDAOWANG.m_type);
                    break;
                case 6:
                    isMatch = (userLevel == USERLEVEL_HAIDAOWANG.m_type);
                    break;
            }
            return isMatch;
        }
    }



    /**
     * 获取圈子加入限制用户等级，和后台定义的用户等级不同，但一一对应
     * @param userLevel
     * @return
     */
    public static String getLimitGrade(int userLevel) {
        switch (userLevel) {
            case 0:
                return "无";
            case 1:
                return "水手";
            case 2:
                return "船员";
            case 3:
                return "队长";
            case 4:
                return "副船长";
            case 5:
                return "船长";
            case 6:
                return "海盗王";
            default:
                return "未知";
        }
    }
    public static String getCircleClass(int classId){
        IMConfigPOJO imConfigPOJO = IMConfigYMAL.getIMConfig();
        if (imConfigPOJO==null||imConfigPOJO.getCircleType()==null){
            return "未知";
        }else {
            String name = "未知";
            for (IMConfigPOJO.CircleType circleType : imConfigPOJO.getCircleType()) {
                if (classId==circleType.getId()){
                    name = circleType.getName();
                    break;
                }
            }
            return name;
        }
    }

}
